<?php
namespace app\index\controller;
use think\Controller;
use think\facade\Env;
use util\Log;
use util\WebSocketClient;

class Index extends Controller
{


    /**
     * 显示页面
     *
     * @return void
     */
    public function index()
    {
        $session_id = session('session_id');
        if (!$session_id) {
            session('session_id', md5('123456qaz'));
        }
        return $this->fetch();
    }

    /**
     * swoole 创建二维码 
     *
     * @return void
     */
    public function swoole()
    {
        $session_id = $_COOKIE['PHPSESSID']; //linux下获取
        $web_socket_client = new WebSocketClient();
        $web_socket_client->connect('localhost', 9500, '/');
        $send_arr['session_id'] = $session_id;
        $send_arr['type'] = 1; //代表创建新的进程来启动
        $rs = $web_socket_client->sendData(json_encode($send_arr));
        if ($rs !== true) {
            // echo "sendData error...\n";
        } else {
            sleep(1);
            $result = cache($session_id); //返回存储在唯一标识session_id的作为key的结果
            if ($result['code'] == 1) {
                $this->assign([
                    'result' => $result,
                    'session_id' => $session_id,
                ]);
                return $this->fetch('code');
            } else {
                Log::info('错误原因:' . $result['msg']);
                $this->error('生成二维码失败，请重试！', url('index/Index/index'));
            }
        }
        $web_socket_client->disconnect();
    }
   

    /**
     * 轮询判断是否通过二维码登录成功
     *
     * @return void
     */
    public function qrLogin()
    {
        $data = request()->param();
        $result = cache('login' . $data['session_id']);

        Log::info('loginres'.var_export($result,true));

        if (empty($result)) {
            $result['code'] = -1;
            $result['msg'] = 'no scan qrcode';
        }
        if ($result['code'] == 1) {
             //获取用户信息
            $user_info['my']  = $result['my'];
           //$user_info['groups']  = $result['myself'];
             cache('user_info' . $data['session_id'], $user_info);
            //清除登陆成功缓存
             cache('login' . $data['session_id'], null);
        }
        return json($result);
    }

    
    /**
     * 成功登录获取个人信息
     *
     * @return void
     */
    public function getMyInfo()
    {
        $session_id = $this->getSessionId();
        $user_info = $this->isCacheByKey('user_info' . $session_id);
        if (!$user_info) {
            $this->error('你还没有登录！', url('index/Index/index'));
        }
        $this->assign([
            'user' => $user_info['my'],
       ]);
        return $this->fetch('myinfo');
    }
    

    /**
     *判断用户是否存在缓存
     *
     * @return boolean
     */
    private function isCacheByKey($key)
    {
        $result = cache($key);
        return $result;
    }
 

       /**
     * 获取session_id
     *
     * @return void
     */
    private function getSessionId()
    {
        if (substr(php_uname(), 0, 7) == "Windows") {
            $session_id = $_COOKIE['PHPSESSID']; //windows下获取
        } else {
            $session_id = $_COOKIE['PHPSESSID']; //linux下获取
        }
        return $session_id;
    }
}
